package cn.net.cfss.fgbp.provider.manage.utils;


import cn.net.cfss.fgbp.provider.manage.support.email.Email;
import cn.net.cfss.fgbp.provider.manage.support.email.EmailSender;
import org.apache.commons.lang.StringUtils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;

/**
 * 发送邮件辅助类
 *
 * @author zyd
 * @version $Id: MailUtil.java, v 0.1 2014年12月4日 下午8:22:43 zyd Exp $
 */
public final class EmailUtil {

    private static String SMTP = "smtp.exmail.qq.com";
    private static String FROM = "fgbp@cfss.net.cn";
    private static String NAME = "fgbp@cfss.net.cn";
    private static String PASSWORD = "GFbp753852";
    private static String CODE = "GFbp753852";

    static {
        Properties properties = new Properties();
        InputStream in = null;
        try {
            in = EmailUtil.class.getResourceAsStream("/config/system.properties");
            properties.load(in);
            SMTP = properties.getProperty("email.smtp.host");
            FROM = properties.getProperty("email.send.from");
            NAME = properties.getProperty("email.user.name");
            PASSWORD = properties.getProperty("email.user.password");
            CODE = properties.getProperty("email.authorisation.code");
        } catch (FileNotFoundException e) {
            System.out.println("找不到配置文件");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private EmailUtil() {
    }

    /**
     * 发送邮件
     */
    public static final boolean sendEmail(Email email) throws Exception {
        email.setHost(SMTP);
        email.setFrom(FROM);
        email.setName(NAME);
        email.setPassword(PASSWORD);
        email.setKey(CODE);
        // 初始化邮件引擎
        EmailSender sender = new EmailSender(email.getHost());
        sender.setNamePass(email.getName(), email.getPassword(), email.getKey());
        if (sender.setFrom(email.getFrom()) == false)
            return false;
        if (sender.setTo(email.getSendTo()) == false)
            return false;
        if (email.getCopyTo() != null && sender.setCopyTo(email.getCopyTo()) == false)
            return false;
        if (sender.setSubject(email.getTopic()) == false)
            return false;
        if (sender.setBody(email.getBody()) == false)
            return false;
        if (email.getFileAffix() != null) {
            for (int i = 0; i < email.getFileAffix().length; i++) {
                if (sender.addFileAffix(email.getFileAffix()[i]) == false)
                    return false;
            }
        }
        // 发送
        return sender.sendout();
    }

    /**
     * 初始化Email对象
     *
     * @return
     */
    public static Email initEmail() {
        Email email = new Email();
        email.setHost(SMTP);
        email.setFrom(FROM);
        email.setName(NAME);
        email.setPassword(PASSWORD);
        email.setKey(CODE);
        return email;
    }

    /**
     * 使用参数匹配模板生成邮件内容
     *
     * @param content
     * @param paramMap
     * @return
     */
    public static String parseTmpl(String content, Map<String, Object> paramMap) {
        if (StringUtils.isNotEmpty(content)) {
            for (Object k : paramMap.keySet()) {
                content = content.replaceAll("\\$\\{" + k + "}", paramMap.get(k).toString());
            }
        }
        return content;
    }

}
